package com.ctim.web.user;

import com.ctim.common.exception.EnumPcsService;
import com.ctim.common.pcs.PcsResult;
import com.ctim.common.tool.json.JSONUtil;
import com.ctim.model.user.User;
import com.ctim.service.bean.UserBean;
import com.ctim.service.user.UserService;
import com.ctim.web.common.BaseController;
import com.ctim.web.util.EncryptUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * Author: Wangwei
 * Date: 2016/8/29
 * Desc:
 */
@RestController
@RequestMapping("/rest/user")
@RequiresRoles("admin:*")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    @RequiresUser
    public PcsResult info() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        return newResult().setData(JSONUtil.filterIncludeProperties(getUser(), User.class,
                "userId", "username", "email", "phone", "menuTree", "permissions","tid", "manager", "defaultIndexUrl"));
    }

    @RequestMapping(value = "",method = RequestMethod.GET)
    public PcsResult userList(@RequestParam("username") String username,
                                  @RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {
        return userService.getUsers(username,getCurPage(pageIndex), getPageSize(pageSize));
    }


    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public PcsResult createUser(UserBean userBean) {
        Integer userId = getUserId();
        userBean.setCreateUid(userId);
        userBean.setPassword(EncryptUtil.md5(userBean.getPassword()));
        userService.createUser(userBean);
        return newResult();
    }

    @RequiresPermissions("user:update")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public PcsResult updateUser( UserBean userBean) {
        Assert.notNull(userBean.getUserId(),"用户ID不能为空");
        Integer userId = getUserId();
        userBean.setUpdateUid(userId);
        if (StringUtils.isNotEmpty(userBean.getPassword())) {
            userBean.setPassword(EncryptUtil.md5(userBean.getPassword()));
        }
        userService.updateUser(userBean);
        return newResult();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public PcsResult deleteUser(@RequestParam("userId") Integer userId) {
        Assert.notNull(userId,"用户ID不能为空");
        boolean ret = userService.deleteUser(userId,getUserId());
        return ret?newResult():failedResult(EnumPcsService.ERROR_OPERATE);
    }

    @RequestMapping(value = "/editpass",method = RequestMethod.POST)
    public PcsResult editpass(@RequestParam("oldpassword") String oldpassword,@RequestParam("newpassword") String newpassword) {
        User user = getUser();
        Integer userId=user.getUserId();
        String password=user.getPassword();
        if(!password.equals(EncryptUtil.md5(oldpassword))){
            return failedResult(EnumPcsService.BUSINESS_USER_PASSWORD_ERROR);
        }
        String encryptPassword = EncryptUtil.md5(newpassword);
        Integer alterRows= userService.editpass(userId,encryptPassword);
        return  (alterRows>0)?newResult().setMessage("密码修改成功！"):failedResult(EnumPcsService.BUSINESS_ALTER_USER_PASSWORD_ERROR);
    }

}
